package com.hcb.tonguetiphealth.manager;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;

import com.hcb.tonguetiphealth.view.R;

public class ReadCityFromXml {
	private Activity ctx;
	private Map<String, String> cityMap;

	public ReadCityFromXml(Context context) {
		ctx = (Activity) context;
	}

	public Map<String, String> getCitys() {
		cityMap = new HashMap<String, String>();
		// 得到Resources资源
		Resources r = ctx.getResources();
		// 通过Resources，获得XmlResourceParser实例
		XmlResourceParser xrp = r.getXml(R.xml.citys);
		try {
			// 如果没有到文件尾继续执行
			while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
				// 如果是开始标签
				if (xrp.getEventType() == XmlResourceParser.START_TAG) {
					// 获取标签名称
					String name = xrp.getName();
					// 判断标签名称是否等于friend
					if (name.equals("city")) {
						// 获得标签属性追加到StringBuilder中
						// 实例化StringBuilder
						String city = xrp.getAttributeValue(0);
						String cityId = xrp.getAttributeValue(1);
						cityMap.put(city, cityId);
					}
				} else if (xrp.getEventType() == XmlPullParser.END_TAG) {
				} else if (xrp.getEventType() == XmlPullParser.TEXT) {
				}
				// 下一个标签
				xrp.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cityMap;
	}
}
